// 本地 临时存储
function getSdata(k, isJson) {
    if (!sessionStorage[k]) return false;
    if (sessionStorage[k] == "undefined") return {};
    var k;
    if (!isJson) {
        try {
            k = JSON.parse(sessionStorage[k])
        } catch (e) {
            k = sessionStorage[k]
        }
    } else {
        k = sessionStorage[k]
    }
    return k
}
function setSdata(k, v){
    if(typeof v != 'string'){
        v = JSON.stringify(v)
    }
    sessionStorage[k] = v;
}
function delSdata(k) {
    delete sessionStorage[k];
}
/**
 * 功能:设置得变量name的值,存储到cookie中
 * @param {[string]} name  [参数名]
 * @param {[string]} value [参数值]
 * @param {[string]} url   [接收的页面]
 * @param {[string]} hour   [保存的小时数]
 * 实例:setCookie('username','baobao')
 */
function setCookie(name,value,hour) {
    if(value == '{}')return
    hour = hour >= 0 ? hour : 10000;
    value = typeof value === 'string'?  value : JSON.stringify(value);
    var exp  = new Date();
    exp.setTime(exp.getTime() + (hour*60*60*1000));
    /*if(debug && location.host != 'api.juewei.com'){
     document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString() + ";path=/;";
     }else {
     document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString() + ";path=/;domain=juewei.com";
     }*/
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString() + ";path=/;domain=xhs1985.com";
}
function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null)
        try{
            document.cookie= name + "="+cval+";expires="+exp.toGMTString();
            document.cookie= name + "="+cval+";expires="+exp.toGMTString()+ ";path=/;domain=xhs1985.com";
        } catch(e) {
            document.cookie= name + "="+cval+";expires="+exp.toGMTString();
        }

}
/**
 * 功能:取得cookie中 变量name的值
 * @param  {[string]} name [参数名]
 * @return {[string]}      [返回参数值]
 * 实例:alert(getCookie("baobao"));
 */
function getCookie(name) {
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
    if(name === 'commonable') {try{return JSON.parse(unescape(arr[2]));}catch(e) {console.log('获取失败')}};
    if(arr !=null) {try{return JSON.parse(unescape(arr[2]));}catch(e) {console.log('json 获取失败'); return unescape(arr[2]); }}; return null;
}
// 末尾金额去零 如果带小数点 就去小数点
var moneyReZero = function(m){
    if(typeof m == "number") { m = m.toFixed(2);}
    if(!/\./g.test(m))return m
    var money = m.replace(/0$/g,"")
    if(/0$/g.test(money)){
        money =  moneyReZero(money)
    }
    money = money.replace(/\.$/,"");
    return money || 0;
}
$.extend({
   toast: function(msg,time){
       var $toast = $('<div class="modal toast">'+msg+'</div>').appendTo(document.body);
       $toast.show();
       $toast.css({
           marginTop: - Math.round($toast.outerHeight() / 2) + 'px',
           marginLeft: - Math.round(parseInt(window.getComputedStyle($toast[0]).width) / 2)  + 'px'
       });
       $toast.addClass('modal-in');
       setTimeout(function () {
           $toast.removeClass('modal-in').addClass('modal-out').remove();
       },time || 2000);
   }
});

